package com.lixin.myorm;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;

public class MainApplication {
    public static void main(String[] args) {

        try {
            // 通过反射获取User的Class对象
            Class<?> userClass = Class.forName("com.lixin.myorm.User");

            // 获取User的所有类注解
            /*Annotation[] classAnnotations = userClass.getAnnotations();
            System.out.println(Arrays.toString(classAnnotations));*/

            // 获取User的@Table类注解
            Table tableAnnotation = userClass.getAnnotation(Table.class);
            String tableName = tableAnnotation.value();
            System.out.println("表名：" + tableName);

            // 获取User的name属性的@Column注解
            Field nameField = userClass.getDeclaredField("name");
            //nameField.setAccessible(true);
            Column nameFieldAnnotation = nameField.getAnnotation(Column.class);
            String tableNameField = nameFieldAnnotation.value();
            String tableNameFieldType = nameFieldAnnotation.type();
            int tableNameFieldLength = nameFieldAnnotation.length();
            System.out.println("字段：" + tableNameField + " " + tableNameFieldType + " " + tableNameFieldLength);

            // 获取User的age属性的@Column注解
            Field ageField = userClass.getDeclaredField("age");
            Column ageFieldAnnotation = ageField.getAnnotation(Column.class);
            String tableAgeField = ageFieldAnnotation.value();
            String tableAgeFieldType = ageFieldAnnotation.type();
            int tableAgeFieldLength = ageFieldAnnotation.length();
            System.out.println("字段：" + tableAgeField + " " + tableAgeFieldType + " " + tableAgeFieldLength);
        } catch (ClassNotFoundException | NoSuchFieldException e) {
            e.printStackTrace();
        }

    }
}
